package com.likeyoudo.views.activity;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.likeyoudo.common.BaseActivity;
import com.likeyoudo.common.constant.HttpStatus;
import com.likeyoudo.common.msg.MsgEvent;
import com.likeyoudo.views.R;
import com.likeyoudo.views.api.LoginApi;
import com.likeyoudo.views.databinding.ActivityMineBinding;
import com.likeyoudo.views.type.entity.system.Role;
import com.likeyoudo.views.type.entity.system.User;
import com.likeyoudo.views.type.response.BaseResponse;
import com.likeyoudo.views.type.response.GetInfoResponse;
import com.likeyoudo.views.utils.HttpRequest;
import com.likeyoudo.views.utils.LoginUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.stream.Collectors;

import cn.hutool.core.util.StrUtil;

/**
 * @author LikeYouDo
 * @date 2024/1/4 21:22
 */
public class MineActivity extends BaseActivity {

    private ActivityMineBinding binding;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_mine;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMineBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        initView();
    }

    private void initView() {
        setImmersiveWindows();
        showBackButton();
        setTitle("我的");

        // 获取用户信息
        LoginApi.getInfo();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(MsgEvent event) {
        String type = event.getType();
        if (LoginApi.LOGOUT.equals(type)) {
            BaseResponse response = event.getResponse(BaseResponse.class);
            if (response.getCode() == 200) {
                LoginUtils.logout();
            }
        } else if (LoginApi.GET_INFO.equals(type)) {
            GetInfoResponse response = event.getResponse(GetInfoResponse.class);
            if (response.getCode() == HttpStatus.SUCCESS) {
                User user = response.getUser();
                binding.userName.setText(user.getUserName());
                binding.nickName.setText(user.getNickName());
                binding.phoneNumber.setText(user.getPhonenumber());
                binding.email.setText(user.getEmail());
                binding.dept.setText(user.getDept().getDeptName());
                binding.role.setText(user.getRoles().stream().map(Role::getRoleName).collect(Collectors.joining(" / ")));
                if (StrUtil.isNotBlank(user.getAvatar())) {
                    Glide.with(this).load(HttpRequest.HOST + user.getAvatar()).centerCrop().into(binding.profile);
                }
            } else {
                ToastUtils.showLong(response.getMsg());
            }
        }
    }

    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.logout) {
            // 退出登录
            LoginApi.logout();
        }
    }
}
